package com.example.springbott.controller;

import com.example.springbott.aspactJ.OperationName;
import com.example.springbott.bean.BaseRespVo;
import com.example.springbott.bean.MarketCategory;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.silic.CategoryListVo;
import com.example.springbott.bean.silic.MarketCategoryVo;
import com.example.springbott.service.AdminCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商场管理——商品类目
 *
 * @author silic xia
 * @since 2022/11/21 17:46
 */
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;


    /**
     * 商品类目List
     *
     * @param /null [id=pid]
     * @return data: commonData -- 商品类目名 -- 具体商品
     * errmsg: "成功"
     * errno: 0
     * @author silic xia
     * @since 2022/11/21 17:48
     */
    @RequiresPermissions("admin:category:list")
    @RequestMapping("list")
    @OperationName("1")
    public BaseRespVo adminCategoryList() {
        CommonData<MarketCategory> data = adminCategoryService.adminCategoryList();
        return BaseRespVo.ok(data);
    }

    /**
     * L1模块
     *
     * @param /null
     * @return data: {total: 10, pages: 1, limit: 10, page: 1,…}
     * errmsg: "成功"
     * errno: 0
     * @author silic xia
     * @since 2022/11/21 19:46
     */
    @RequestMapping("l1")
    @OperationName("1")
    public BaseRespVo adminCategoryL1() {
        CommonData<CategoryListVo> data = adminCategoryService.adminCategoryL1();
        return BaseRespVo.ok(data);
    }

    /**
     * 新增商品类目
     *
     * @param {"name":"大麻哈","keywords":"555","level":"L1","pid":0,"desc":"88888","iconUrl":"","picUrl":""}
     * @return {"errno":0,"data":{"id":1036045,"name":"大麻哈","keywords":"555","desc":"88888","pid":0,"iconUrl":"",
     * "picUrl":"","level":"L1","addTime":"2022-11-21 20:06:23","updateTime":"2022-11-21 20:06:23"},"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 20:06
     */
    @RequiresPermissions("admin:category:create")
    @RequestMapping("create")
    @OperationName("1")
    public BaseRespVo adminCategoryCreate(@RequestBody MarketCategory marketCategory) {
        MarketCategoryVo data = adminCategoryService.adminCategoryCreate(marketCategory);
        return BaseRespVo.ok(data);
    }

    /**
     * 修改商品update——这里修改了一个老师案例的Bug
     * 【把一级类目修改为二级类目，父类目修改为当前类目，数据会被折叠消失】
     *
     * @param /children: [{id: 1036047, name: "拉伯", keywords: "阿丁", desc: "2222", iconUrl: "", picUrl: "", level: "L2"}]
     *                   desc: "88888"
     *                   iconUrl: ""
     *                   id: 1036045
     *                   keywords: "6666"
     *                   level: "L1"
     *                   name: "大麻哈"
     *                   picUrl: ""
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 20:36
     */
    @RequiresPermissions("admin:category:update")
    @RequestMapping("update")
    @OperationName("1")
    public BaseRespVo adminCategoryUpdate(@RequestBody MarketCategory marketCategory) {
        Integer id = marketCategory.getId();
        Integer pid = marketCategory.getPid();
        if (id != null || pid != null){
            if (id.equals(pid)) {
                return BaseRespVo.invalidParameter402("类目级别有问题！不能为当前类目...");
            }
        }
        adminCategoryService.adminCategoryUpdate(marketCategory);
        return BaseRespVo.ok();
    }

    /**
     * 商品类目——delete
     *
     * @param
     * {"id":1036056,"name":"嗡嗡嗡嗡嗡嗡","keywords":"111",
     * "desc":"111","iconUrl":"","picUrl":"","level":"L1","children":[]}
     * @return {"errno":0,"errmsg":"成功"}
     *
     * @author  silic xia
     * @since 2022/11/21 21:20
     */
    @RequiresPermissions("admin:category:delete")
    @RequestMapping("delete")
    @OperationName("1")
    public BaseRespVo adminCategoryDelete(@RequestBody MarketCategory marketCategory){
        adminCategoryService.adminCategoryDelete(marketCategory);
        return BaseRespVo.ok();
    }
}

